using System.Collections.Generic;
/// <summary>
/// MonoBehaviour单例管理器
/// </summary>
public sealed partial class HFSingleMonoBehaviourManager
{
    /// <summary>
    /// 单例缓存
    /// </summary>
    static Dictionary<int, IHFObject> mSingleCache = new Dictionary<int, IHFObject>();
    /// <summary>
    /// 添加单例
    /// </summary>
    /// <typeparam name="T">单例类型</typeparam>
    /// <param name="_instance">单例</param>
    public static void AddSingle<T>(T _instance) where T : IHFObject, new()
    {        
        int tid = typeof(T).GetHashCode();
        if (!mSingleCache.ContainsKey(tid))
        {
            mSingleCache[tid] = _instance;
        }
#if UNITY_EDITOR
        else
        {
            HFP.HFError($"AddSingle has the same type single. Type {_instance}");
        }
#endif
    }

    /// <summary>
    /// 获得指定单例
    /// </summary>
    /// <typeparam name="T">单例类型</typeparam>
    /// <returns>单例</returns>
    public static T GetSingle<T>() where T : IHFObject, new()
    {
        int tid = typeof(T).GetHashCode();
        IHFObject result = null;
        mSingleCache.TryGetValue(tid, out result);
        return (T)result;
    }

    /// <summary>
    /// 是否有指定单例
    /// </summary>
    /// <typeparam name="T">单例类型</typeparam>
    /// <returns>true:有,false:否</returns>
    public static bool HasSingle<T>() where T : AbsHFSingleMonoBehaviour<T>, new()
    {
        int tid = typeof(T).GetHashCode();
        return mSingleCache.ContainsKey(tid);
    }
}
